<?php
namespace clt;
class Menu{
	private $rules = null;
	private $adminID = 1;
	public function getMenus(&$navList=[]){
		//导航
		$this->getAdminRules();//获取权限列表
        // 获取缓存数据
        $authRule = cache('authRule');
        if(!$authRule){
            $authRule = db('auth_rule')->where('menustatus=1')->order('sort')->select();
            cache('authRule', $authRule, 3600);
       }
       $uri = request()->controller()."/".request()->action();
       $currentURL = strtolower(U($uri));
        //声明数组
        $spread = false;
        $menus = array();
        $menus[] = array(
            'id'=>-1,
            'href' => U('index/index'),
            'title'=>'Dashboard',
            'icon' => 'icon-home',
            'pid' => 0,
        );
        foreach ($authRule as $key=>$val){
            $val['href']=$authRule[$key]['href'] = U($val['href']);
            if($val['pid']==0){
                if(session('aid')!=$this->adminID){
                    if(in_array($val['id'],$this->rules)){
                        $menus[] = $val;
                    }
                }else{
                    $menus[] = $val;
                }
            }
            
        }
        foreach ($menus as $k=>$v){
            if(!$spread && $currentURL == strtolower($v['href'])){
                $menus[$k]['spread'] = true;
                $vv['spread'] = true;
                $spread = true;
                $navList[] = $v['title'];
            }
            foreach ($authRule as $kk=>$vv){

                if($v['id']==$vv['pid']){
                    if($currentURL == strtolower($vv['href'])){
                        $menus[$k]['spread'] = true;
                        $vv['spread'] = true;
                        $spread = true;
                        $navList[] = $v['title'];
                        //$navList[] = $vv['title'];
                    }else{
                        $map['href'] = $uri;
                        $s = $vv['id'];
                        $map['pid'] = array('in',explode("|", $s ));
                        $men = db('auth_rule')->where($map)->find();
                        if($men){
                            $navList[] = $v['title'];
                            $navList[] = $men['title'];
                            $vv['spread'] = true;
                            $spread = true;
                            $menus[$k]['spread'] = true;
                        }
                        
                    }
                    if (in_array($vv['id'], $this->rules)) {
                        $menus[$k]['children'][] = $vv;
                    }
                    // if(session('aid')!=$this->adminID) {
                    //     if (in_array($vv['id'], $this->rules)) {
                    //         $menus[$k]['children'][] = $vv;
                    //     }
                    // }else{
                    //     $menus[$k]['children'][] = $vv;
                    // }
                }
                
            }

        }
        
        //$menus[0]['spread'] = $spread ? false : true;
        //var_dump($menus);
        return $menus;
	}
	/*获取登录者的权限列表*/
	private function getAdminRules(){
		//当前管理员权限
        $map['a.admin_id'] = session('aid');
        $rules=db('Users')->alias('a')
            ->join('auth_group ag','a.group_id = ag.group_id','left')
            ->where($map)
            ->value('ag.rules');
        $this->rules = explode(',',$rules);
	}
}